﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanLyDuoc.HeThongNhapXuat
{
   public class PhieuXuatKhac:DBInteractionBase
   {
       // Phiếu xuất khác
       public string MaPhieu { get; set; }
       public string MaKho { get; set; }
       public DateTime NgayGiao { get; set; }
       public string NguoiGiao { get; set; }
       public string NguoiNhan { get; set; }
       public string DienGiai { get; set; }
       public string MaNguoiXetDuyet { get; set; }
       public int IsXetDuyet { get; set; }
       public string NguoiTao { get; set; }
       // Phiếu xuất khác chi tiết
       public string ID { get; set; }
       public string MaVatTu { get; set; }
       public string MaDonVi { get; set; }
       public int SoLuong { get; set; }
       public double DonGia { get; set; }
       public double ThanhTien { get; set; }
       public string LoSanXuat { get; set; }
       public DateTime HanSuDung { get; set; }
       public string MaPhieuNhap { get; set; }

       public bool PhieuXuatKhac_Insert(PhieuXuatKhac obj)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_Insert", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu_Output", SqlDbType.VarChar)
                   {
                       Direction = ParameterDirection.Output,
                       Size = 50
                   });
                   dbcmd.Parameters.Add(new SqlParameter("@NgayGiao", obj.NgayGiao));
                   dbcmd.Parameters.Add(new SqlParameter("@MaKho", obj.MaKho));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiGiao", obj.NguoiGiao));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiNhan", obj.NguoiNhan));
                   dbcmd.Parameters.Add(new SqlParameter("@DienGiai", obj.DienGiai));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                   dbcmd.ExecuteNonQuery();
                   obj.MaPhieu = dbcmd.Parameters["@MaPhieu_Output"].Value.ToString();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public bool PhieuXuatKhac_Update(PhieuXuatKhac obj)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_Update", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                   dbcmd.Parameters.Add(new SqlParameter("@MaKho", obj.MaKho));
                   dbcmd.Parameters.Add(new SqlParameter("@NgayGiao", obj.NgayGiao));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiGiao", obj.NguoiGiao));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiNhan", obj.NguoiNhan));
                   dbcmd.Parameters.Add(new SqlParameter("@DienGiai", obj.DienGiai));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                   dbcmd.ExecuteNonQuery();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public bool PhieuXuatKhac_Delete(string id)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_Delete", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", id));
                   dbcmd.ExecuteNonQuery();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public bool PhieuXuatKhac_UpdateDuyetPhieu(string maphieu, string manguoixetduyet, int isxetduyet, string ghichu)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_UpdateDuyetPhieu", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", maphieu));
                   dbcmd.Parameters.Add(new SqlParameter("@MaNguoiXetDuyet", manguoixetduyet));
                   dbcmd.Parameters.Add(new SqlParameter("@IsXetDuyet", isxetduyet));
                   dbcmd.Parameters.Add(new SqlParameter("@GhiChu", ghichu));
                   dbcmd.ExecuteNonQuery();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public DataTable PhieuXuatKhac_SelectById(string id)
       {
           var dt = new DataSet();
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_SelectById", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", id));
                   var dtAdapter = new SqlDataAdapter(dbcmd);
                   dtAdapter.Fill(dt);
                   return dt.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw new Exception("tbDuoc_PhieuXuatKhac_SelectById: " + ex.Message);
           }
       }

       public DataTable PhieuXuatKhac_SelectAll(DateTime tungay, DateTime denngay, string makho, int pageindex, int pagesize, out int recordcount)
       {
           var dt = new DataSet();
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_SelectAll", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@TuNgay", tungay));
                   dbcmd.Parameters.Add(new SqlParameter("@DenNgay", denngay));
                   dbcmd.Parameters.Add(new SqlParameter("@MaKho", makho));
                   dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                   dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                   dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                   {
                       Direction = ParameterDirection.Output
                   });
                   var dtAdapter = new SqlDataAdapter(dbcmd);
                   dtAdapter.Fill(dt);
                   recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                   return dt.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw new Exception("tbDuoc_PhieuXuatKhac_SelectAll: " + ex.Message);
           }
       }
       
       public bool PhieuXuatKhac_ChiTiet_Insert(PhieuXuatKhac obj)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_ChiTiet_Insert", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                   dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", obj.MaVatTu));
                   dbcmd.Parameters.Add(new SqlParameter("@SoLuong", obj.SoLuong));
                   dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                   dbcmd.ExecuteNonQuery();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public bool PhieuXuatKhac_ChiTiet_Delete(string maphieu, string mavattu)
       {
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_ChiTiet_Delete", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhac", maphieu));
                   dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", mavattu));
                   dbcmd.ExecuteNonQuery();
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }

       public DataTable PhieuXuatKhac_ChiTiet_SelectByMaPhieu(string maphieu)
       {
           var dt = new DataSet();
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieu", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhac", maphieu));
                   var dtAdapter = new SqlDataAdapter(dbcmd);
                   dtAdapter.Fill(dt);
                   return dt.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw new Exception("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieu: " + ex.Message);
           }
       }

       public DataTable PhieuXuatKhac_ChiTiet_SelectByMaPhieu_ChiTiet(string maphieu)
       {
           var dt = new DataSet();
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieu_ChiTiet", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhac", maphieu));
                   var dtAdapter = new SqlDataAdapter(dbcmd);
                   dtAdapter.Fill(dt);
                   return dt.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw new Exception("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieu: " + ex.Message);
           }
       }

       public DataTable PhieuXuatKhac_ChiTiet_SelectByMaPhieuMaVatTu(string maphieu, string mavattu)
       {
           var dt = new DataSet();
           try
           {
               using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieuMaVatTu", GetConnection()))
               {
                   dbcmd.CommandType = CommandType.StoredProcedure;
                   dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhac", maphieu));
                   dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", mavattu));
                   var dtAdapter = new SqlDataAdapter(dbcmd);
                   dtAdapter.Fill(dt);
                   return dt.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw new Exception("tbDuoc_PhieuXuatKhac_ChiTiet_SelectByMaPhieu: " + ex.Message);
           }
       }
    }
}
